package com.bfsuol.yihang.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bfsuol.bfsuolframework.core.controller.SpringControllerSupport;
import com.bfsuol.bfsuolframework.core.controller.json.MessageJson;
import com.bfsuol.bfsuolframework.core.controller.support.Pagination;
import com.bfsuol.bfsuolframework.core.controller.support.QueryParam;
import com.bfsuol.bfsuolframework.core.spring.QueryParameter;
import com.bfsuol.bfsuolframework.core.utils.StringUtils;
import com.bfsuol.bfsuolframework.security.annotations.*;
import com.bfsuol.bfsuolframework.security.dao.Privilege;
import com.bfsuol.bfsuolframework.security.dao.RolePrivilege;
import com.bfsuol.bfsuolframework.security.service._PrivilegeService;
import com.bfsuol.yihang.customers.api.CommandCustomerService;
import com.bfsuol.yihang.customers.api.QueryCustomerService;
import com.bfsuol.yihang.customers.dto.CustomerDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@Controller
@RequestMapping("/yihang/customer")
public class CustomerController extends SpringControllerSupport {

    @Autowired
    private CommandCustomerService commandCustomerService;
    @Autowired
    private QueryCustomerService queryCustomerService;

    @SecPrivilege(title = "客户管理")
    @RequestMapping("/index")
    public ModelAndView index(Model model){
        model.addAttribute("statusList",CustomerDTO.getStatusList());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(getViewNamedPath("index"));
        return modelAndView;
    }

    @SecList
    @RequestMapping("/list")
    public ModelAndView list(
            CustomerDTO customerDTO,
            @ModelAttribute("page") Pagination page){

        com.baomidou.mybatisplus.extension.plugins.pagination.Page page1 = new Page(page.getDefaultCurrentPage(),page.getPageSize());
        com.baomidou.mybatisplus.extension.plugins.pagination.Page customerDTOPage = queryCustomerService.page(page1,customerDTO);
        page.setItems(customerDTOPage.getRecords());
        page.setTotalRecord((int) customerDTOPage.getTotal());

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(getViewNamedPath("list"));
        return modelAndView;
    }

    @RequestMapping("/input")
    public ModelAndView create(@ModelAttribute CustomerDTO customerDTO, Model model){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(getViewNamedPath("input"));
        return modelAndView;
    }

    @RequestMapping("/input/{id}")
    public ModelAndView edit(@PathVariable("id") Long id, Model model){
        CustomerDTO customerDTO = queryCustomerService.getById(id);
        model.addAttribute("customer",customerDTO);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(getViewNamedPath("input"));
        return modelAndView;
    }

    @SecCreate
    @SecEdit
    @RequestMapping("/save")
    public @ResponseBody
    MessageJson save(@ModelAttribute CustomerDTO customerDTO){
        if(customerDTO.getStatus()==null) return MessageJson.fail("必须设置客户类型");
        if(StringUtils.isBlank(customerDTO.getName())) return MessageJson.fail("客户姓名不能为空");

        if(customerDTO.isNew()){
            customerDTO.setCreateTime(new Date());
            customerDTO.setUpdateTime(new Date());
            customerDTO.setCreateUserName(this.getLoginInfo().getLoginName());
            customerDTO.setUpdateUserName(this.getLoginInfo().getLoginName());
            commandCustomerService.save(customerDTO);
        }else{
            customerDTO.setUpdateTime(new Date());
            customerDTO.setUpdateUserName(this.getLoginInfo().getLoginName());
            commandCustomerService.updateById(customerDTO);
        }
        return MessageJson.information("保存成功");
    }

    @SecDelete
    @RequestMapping("/delete/{id}")
    public @ResponseBody
    MessageJson delete(@PathVariable("id") Long id) {
        if(id != null){
            commandCustomerService.deleteById(id);
        }
        return MessageJson.information("删除成功");
    }
}
